package com.nuoniu.sibanyun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.SysMenu;
import com.nuoniu.sibanyun.entity.vo.AppUserMenuVo;
import com.nuoniu.sibanyun.entity.vo.AppUserModuleVo;
import com.nuoniu.sibanyun.entity.vo.RouterMenuVo;
import com.nuoniu.sibanyun.mapper.baseMapper.TreeBaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 菜单表 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@Mapper
@Component
public interface SysMenuMapper extends BaseMapper<SysMenu> , TreeBaseMapper {

    @Select("SELECT m.*  FROM sys_menu m INNER JOIN sys_role_menu rm ON rm.menu_id = m.id " +
            " INNER JOIN sys_user_role ur ON ur.role_id = rm.role_id AND ur.user_id = #{userId}" +
            " AND m.`status` = 0" +
            " INNER JOIN sys_role r ON r.id = ur.role_id AND r.`status` = 0 ")
    List<SysMenu> listByUserId(@Param("userId") Long userId);

    /**
     * 查询 菜单路由信息
     * @param userId
     * @return
     */
    List<RouterMenuVo> listRouterMenuVoByUserId(@Param("userId") Long userId);

    List<AppUserMenuVo> listAppUserModuleVoByUserId(Long userId);

    List<RouterMenuVo> listRouterMenuVoBySystem();


    /**
     * 根据uri获取菜单
     * @author 小啊刚
     * @date 2020/5/22
     */
    @Select("select * from sys_menu where menu_href = #{uri} limit 1")
    SysMenu getByUri(String uri);


    boolean isHaveChildrenMenus(@Param("menuId") Long menuId);

    /**
     * 根据模块下菜单列表
     * @author 小啊刚
     * @date 2020/5/26
     */
    @Select("select * from sys_menu where module_codes like BINARY \"%\"#{moduleCode}\"%\"")
    List<SysMenu> getByModule(String moduleCode);

    /**
     * 根据模块修改状态
     * @author 小啊刚
     * @date 2020/5/26
     */
    @Update("update sys_menu set status = #{status} where module_codes like \"%\"#{moduleCode}\"%\"")
    void updateStatus(String moduleCode, int status);

    /**
     * 获取角色授权菜单id
     * @author 小啊刚
     * @date 2020/5/28
     */
    @Select("SELECT m.id , m.tree_level , m.tree_leaf , m.menu_name ,parent_ids   from sys_menu m INNER JOIN sys_role_menu rm ON rm.menu_id = m.id  WHERE role_id = #{id} order by m.tree_sort")
    List<SysMenu> getByRoleList(@Param("id") Long id);

    /**
     * 获取角色授权菜单
     * @author 小啊刚
     * @date 2020/5/28
     */
    @Select("SELECT m.* from sys_menu m INNER JOIN sys_role_menu rm ON rm.menu_id = m.id  WHERE role_id = #{id} and m.status=0 order by m.tree_sort")
    List<SysMenu> getByRoleMenu(@Param("id") Integer id);

    /**
     * 获取角色授权菜单的ID
     * @param id
     * @return
     */
    @Select("SELECT m.id from sys_menu m INNER JOIN sys_role_menu rm ON rm.menu_id = m.id  WHERE role_id = #{id} and m.status=0")
    List<Long> getMenuIdsByRoleId(@Param("id") Integer id);
    /**
     * 修改菜单模块
     * @author 小啊刚
     * @date 2020/6/12
     */
    @Update("update sys_menu set module_codes = #{newModule} where module_codes = #{oldModule}")
    void updateModule(String newModule, String oldModule);

    /**
     * 获取所有
     * @author 小啊刚
     * @date 2020/6/18
     */
    @Select("SELECT m.* from sys_menu m WHERE m.status=0  order by m.tree_sort")
    List<SysMenu> getAllMenu();
}
